package UseFile;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReadAndWrite {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        PrintWriter writer = null;

        try {
            // 创建 PrintWriter 对象，指定文件名为 output.txt
            writer = new PrintWriter(new FileWriter("output.txt"));

            System.out.println("请输入第一句话:");
            String sentence1 = scanner.nextLine();
            writer.println(sentence1);

            System.out.println("请输入第二句话:");
            String sentence2 = scanner.nextLine();
            writer.println(sentence2);

            System.out.println("两句话已成功写入文件 output.txt。");
        } catch (IOException e) {
            System.err.println("写入文件时出现错误: " + e.getMessage());
        } finally {
            if (writer != null) {
                writer.close();
            }
            scanner.close();
        }
    }
}
